$(function () {
    back_to_cart();
    pay_confirm();
})

function back_to_cart() {
    $('#back-button').click(() => {
        window.location.href = '/page/cart.html';
    });
}

function pay_confirm() {
    $('#pay-confirm').click(() => {
        if($('#location').val() != '' && $('#phone').val() != '' && $('#phone').val().length == 11) {
            $.ajax({
                url: '/switch-cart-to-order',
                type: 'post',
                dataType: 'json',
                data: {
                    location: $('#location').val(), 
                    phone: $('#phone').val(),

                },
                success: function (data) {
                    if (data.status) {
                        $('#pay-confirm').attr('class', 'btn btn-success');
                        $('#pay-confirm').text('支付成功');
                        setTimeout("window.location.href = '/page/user-main.html';", 1000);
                    } else {
                        alert('数据获取失败');
                    }
                }
            });
        } else if($('#phone').val().length != 11) {
            $('#pay-confirm').attr('class', 'btn btn-danger');
            $('#pay-confirm').text('手机号必须为11位');
        } else {
            $('#pay-confirm').attr('class', 'btn btn-danger');
            $('#pay-confirm').text('完善基本信息');
        }
    });
}